from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
import json

def ecs_execl(AccessKeyId,AccessKeySecret,ecsRegionid):
    """返回一个由指定区域所有主机
    [['产品名称', 'ip', 'cpu','内存M', '到期时间'],[],[]]
    构成的大列表
    """
    client = AcsClient(AccessKeyId, AccessKeySecret, ecsRegionid)

    request = DescribeInstancesRequest()
    request.set_accept_format('json')

    request.set_PageSize(100)

    response = client.do_action_with_exception(request)
    # python2:  print(response)
    responseJson = json.loads(str(response, encoding='utf-8'))
    ECS_count = len(responseJson['Instances']['Instance'])
    all_ECS = []
    for i in range(ECS_count):
        ECS_name = responseJson['Instances']['Instance'][i]['InstanceName']
        ECS_IP = responseJson['Instances']['Instance'][i]['EipAddress']['IpAddress']
        ECS_cpu = responseJson['Instances']['Instance'][i]['Cpu']
        ECS_memery = responseJson['Instances']['Instance'][i]['Memory']
        ECS_ExpiredTime = responseJson['Instances']['Instance'][i]['ExpiredTime']
        ECS_OSName = responseJson['Instances']['Instance'][i]['OSName']

        ECS_info = [ECS_name, ECS_IP, ECS_cpu, ECS_memery,ECS_OSName, ECS_ExpiredTime]
        all_ECS.append(ECS_info)
    return all_ECS

